import 'package:jyofservice/util/webconfig.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:dio/dio.dart';
import 'package:jyofservice/bean/work_log_list_entity.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:jyofservice/util/AesUtil.dart';
// import 'package:timeline_list/timeline.dart';
import 'package:timeline_list/timeline_list.dart';
// import 'package:timeline_list/timeline_model.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';

class WorkRecord extends StatefulWidget {
  String? LSH;

  WorkRecord({this.LSH});

  @override
  _WorkRecordState createState() => _WorkRecordState();
}

class _WorkRecordState extends State<WorkRecord> {
  late WorkLogListEntity workLogListEntity;
  List<WorkLogListData>? list = [];
  bool isloading = false;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getWorkRecord();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: const Color(0xFFF1F1F1),
        appBar: AppBar(
          centerTitle: true,
          title: Text('工单日志',
              style: TextStyle(
                  color: Colors.white, fontSize: ScreenUtil().setSp(45))),
        ),
        body: Stack(
          alignment: Alignment(0.5, 0.5),
          children: <Widget>[
            RefreshIndicator(
                child: Timeline.builder(
                  properties: TimelineProperties(
                      iconAlignment: MarkerIconAlignment.center,
                      iconSize: 35.w,
                      timelinePosition: TimelinePosition.start),
                  markerBuilder: (BuildContext context, int index) {
                    bool isContent = false;
                    bool ismove = false;
                    if (list![index].czType == 8 || list![index].czType == 3) {
                      list![index].isContent = false;
                    } else {
                      list![index].isContent = true;
                    }
                    if (list![index].ifflow == 1) {
                      list![index].ismove = false;
                    } else {
                      list![index].ismove = true;
                    }
                    return _buildData(
                        index,
                        list![index].createDate,
                        list![index].czUserName!,
                        list![index].czTypeName!,
                        list![index].dDUserName ?? "",
                        list![index].content!,
                        list![index].ismove,
                        list![index].isContent);
                  },
                  markerCount: list!.length,
                  physics: ClampingScrollPhysics(),
                  context: context,
                ),
                onRefresh: _onRefresh),
            Offstage(
              offstage: isloading,
              child: SpinKitFadingCircle(
                color: Colors.blue,
                size: 50.0,
              ),
            )
          ],
        ));
  }

  //刷新
  Future _onRefresh() async {
    list!.clear();
    getWorkRecord();
  }

  Marker _buildData(int index, String? time, String operatname, String type,
      String move, String content, bool isMove, bool isContent) {
    print("ismove:" + isMove.toString());
    return Marker(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Container(
            padding: EdgeInsets.only(top: 30),
            child: Text(
              time ?? '',
              style: TextStyle(fontSize: ScreenUtil().setSp(40)),
            ),
          ),
          SizedBox(
            height: ScreenUtil().setHeight(5),
          ),
          Container(
            padding: EdgeInsets.all(10),
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(10),
            ),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Text(
                  "操作人：" + operatname ?? '',
                  style: TextStyle(fontSize: ScreenUtil().setSp(35)),
                ),
                SizedBox(
                  height: ScreenUtil().setHeight(10),
                ),
                Text(
                  "操作类型：" + type ?? '',
                  style: TextStyle(fontSize: ScreenUtil().setSp(35)),
                ),
                SizedBox(
                  height: ScreenUtil().setHeight(10),
                ),
                Offstage(
                  offstage: isContent,
                  child: Text(
                    "操作内容：" + content ?? '',
                    style: TextStyle(fontSize: ScreenUtil().setSp(35)),
                  ),
                ),
              ],
            ),
          ),
          Offstage(
            offstage: isMove,
            child: Text(
              "工单移动到=>：" + move ?? '',
              style: TextStyle(
                  color: Colors.blue, fontSize: ScreenUtil().setSp(35)),
            ),
          ),
        ],
      ),
      position: index % 2 == 0 ? MarkerPosition.right : MarkerPosition.left,
      // isFirst: index == 0,
      // isLast: index == list!.length,
      icon: Container(
        width: 35.w,
        height: 35.w,
        decoration: BoxDecoration(color: Colors.white,shape: BoxShape.circle, ),
        child: Icon(
          Icons.adjust,
          size: 26.w,
          color: Colors.blue,
        ),
      ),
      // iconBackground: Color.fromRGBO(255, 255, 255, 1)
    );
  }

  Future getWorkRecord() async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    String? key = sharedPreferences.getString("key");
    String aesData = await AesUtil.encodeBase64();
    var data = {
      "key": key,
      "LSH": widget.LSH,
      "ClientType": "phone",
      "ClientKey": aesData
    };
    print(widget.LSH);
    Response response =
        await Dio().post(WebConfig().baseUrl + "Work/GetWorkRiZhi", data: data);
    print(response.data.toString());

    if (response.data["Code"] == "200") {
      workLogListEntity = new WorkLogListEntity.fromJson(response.data);
    } else {
      Fluttertoast.showToast(msg: "当前请求数据错误请重新刷新");
    }
    print(workLogListEntity.data!.length);
    isloading = true;
    if (mounted) {
      setState(() {
        list = workLogListEntity.data;
      });
    }
  }
}
